package com.drore.app.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Build;
import android.util.DisplayMetrics;


import com.drore.www.logbeansecret.bean.PhoneBean;

import java.util.Locale;
import java.util.TimeZone;

/**
 * 手机信息
 *
 * @author sixbing
 * @date 2018/11/8 11:48
 */

public class PhoneInfoUtil {
    public static String TAG = "PhoneInfoUtil";

    // 用来存储设备信息和异常信息
    public static PhoneBean getPhoneBean(Context ctx) {
        PhoneBean phoneBean = new PhoneBean();
        boolean phoneState = CheckPermissionUtil.chechPermission(ctx, Manifest.permission.READ_PHONE_STATE);
        if (phoneState) {
            phoneBean.setTelephone(AppUtils.getPhoneNum(ctx));
            phoneBean.setIp(AppUtils.getMacAddress(ctx));
        } else {
            phoneBean.setTelephone("");
            phoneBean.setIp("");
            LogUtils.e(TAG, "permission  READ_PHONE_STATE  is not allow ");
        }
        boolean WIFIState = CheckPermissionUtil.chechPermission(ctx, Manifest.permission.ACCESS_WIFI_STATE);
        if (WIFIState) {
            phoneBean.setNetstatus("" + NetWorkUtils.isNetworkConnected(ctx));
            phoneBean.setNettype(NetWorkUtils.getNetWorkType(ctx));
        } else {
            phoneBean.setNetstatus("");
            phoneBean.setNettype("");
            LogUtils.e(TAG, "permission  ACCESS_WIFI_STATE  is not allow ");
        }

        phoneBean.setUserid(Build.USER);
        phoneBean.setGuid("" + Build.HARDWARE);
        phoneBean.setPlatform("android " + Build.VERSION.RELEASE);

        /**
         * 判断当前设备是手机还是平板，代码来自 Google I/O App for Android
         * @param context
         * @return 平板返回 True，手机返回 False
         */
        phoneBean.setIsmobile("" + !((ctx.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE));
        String able = Locale.getDefault().getCountry();
        phoneBean.setLanguage(able);
        DisplayMetrics mDisplayMetrics = ctx.getResources().getDisplayMetrics();
        int width = mDisplayMetrics.widthPixels;
        int height = mDisplayMetrics.heightPixels;
        float density = mDisplayMetrics.density;
        int densityDpi = mDisplayMetrics.densityDpi;
        phoneBean.setDensity(density + "/dpi:" + densityDpi);
        phoneBean.setScreen(height + "×" + width);
        try {
            PackageManager pm = ctx.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(),
                    PackageManager.GET_ACTIVITIES);
            if (pi != null) {
                phoneBean.setAppid("" + pi.versionCode);
            }
        } catch (PackageManager.NameNotFoundException e) {
            LogUtils.e(TAG, "an error occured when collect package info", e);
        }
        phoneBean.setUdid("" + Build.SERIAL);
        phoneBean.setOs("" + Build.FINGERPRINT);
        phoneBean.setSdkversion("" + Build.VERSION.SDK_INT);
        phoneBean.setModule(Build.BRAND + "/" + Build.MODEL);
        phoneBean.setImei(Build.SERIAL);
        phoneBean.setCpu(Build.CPU_ABI + " " + Build.CPU_ABI2);
        TimeZone tz = TimeZone.getDefault();
        String s = "" + tz.getDisplayName(false, TimeZone.SHORT) + "   " + tz.getID();
        phoneBean.setTimezone(s);

        String channel = "";
        try {
            ApplicationInfo info = ctx.getPackageManager().
                    getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
            if (info != null && info.metaData != null) {
                String metaData = info.metaData.getString("CP_CHANNEL");
                if (!metaData.isEmpty()) {
                    channel = metaData;
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        phoneBean.setChannel(channel);
//        getDeviceInfo();
        return phoneBean;
    }

    /**
     * 获取指定字段信息
     *
     * @return
     */
    public static void getDeviceInfo() {
        StringBuffer sb = new StringBuffer();
        sb.append("主板：" + Build.BOARD);
        sb.append("\n系统启动程序版本号：" + Build.BOOTLOADER);
        sb.append("\n系统定制商：" + Build.BRAND);
        sb.append("\ncpu指令集：" + Build.CPU_ABI);
        sb.append("\ncpu指令集2：" + Build.CPU_ABI2);
        sb.append("\n设置参数：" + Build.DEVICE);
        sb.append("\n显示屏参数：" + Build.DISPLAY);
        sb.append("\n无线电固件版本：" + Build.getRadioVersion());
        sb.append("\n硬件识别码：" + Build.FINGERPRINT);
        sb.append("\n硬件名称：" + Build.HARDWARE);
        sb.append("\nHOST:" + Build.HOST);
        sb.append("\n修订版本列表：" + Build.ID);
        sb.append("\n硬件制造商：" + Build.MANUFACTURER);
        sb.append("\n版本：" + Build.MODEL);
        sb.append("\n硬件序列号：" + Build.SERIAL);
        sb.append("\n手机制造商：" + Build.PRODUCT);
        sb.append("\n描述Build的标签：" + Build.TAGS);
        sb.append("\nTIME:" + Build.TIME);
        sb.append("\nbuilder类型：" + Build.TYPE);
        sb.append("\nUSER:" + Build.USER);
        LogUtils.e(TAG, "Build.HOST--->" + sb);
        LogUtils.e(TAG, "Build.VERSION.BASE_OS--->" + Build.VERSION.BASE_OS);
        LogUtils.e(TAG, "Build.VERSION.CODENAME--->" + Build.VERSION.CODENAME);
        LogUtils.e(TAG, "Build.VERSION.INCREMENTAL--->" + Build.VERSION.INCREMENTAL);
        LogUtils.e(TAG, "Build.VERSION.RELEASE--->" + Build.VERSION.RELEASE);
        LogUtils.e(TAG, "Build.VERSION.SECURITY_PATCH--->" + Build.VERSION.SECURITY_PATCH);
        LogUtils.e(TAG, "Build.VERSION.SDK--->" + Build.VERSION.SDK);
        LogUtils.e(TAG, "Build.VERSION.SDK_INT--->" + Build.VERSION.SDK_INT);
        LogUtils.e(TAG, "Build.VERSION.PREVIEW_SDK_INT--->" + Build.VERSION.PREVIEW_SDK_INT);
        LogUtils.e(TAG, "Build.ID--->" + Build.ID);
        LogUtils.e(TAG, "Build.PRODUCT--->" + Build.PRODUCT);
        LogUtils.e(TAG, "Build.DEVICE--->" + Build.DEVICE);
        LogUtils.e(TAG, "Build.BOARD--->" + Build.BOARD);
        LogUtils.e(TAG, "Build.CPU_ABI--->" + Build.CPU_ABI);
        LogUtils.e(TAG, "Build.CPU_ABI2--->" + Build.CPU_ABI2);
        LogUtils.e(TAG, "Build.MANUFACTURER--->" + Build.MANUFACTURER);
        LogUtils.e(TAG, "Build.BRAND--->" + Build.BRAND);
        LogUtils.e(TAG, "Build.MODEL--->" + Build.MODEL);
        LogUtils.e(TAG, "Build.BOOTLOADER--->" + Build.BOOTLOADER);
        LogUtils.e(TAG, "Build.RADIO--->" + Build.RADIO);
        LogUtils.e(TAG, "Build.HARDWARE--->" + Build.HARDWARE);
//        LogUtils.e(TAG, "Build.getSerial()--->" + Build.getSerial());
//        LogUtils.e(TAG, "Build--->" + Build.SUPPORTED_ABIS);
//        LogUtils.e(TAG, "Build--->" + Build.SUPPORTED_32_BIT_ABIS);
//        LogUtils.e(TAG, "Build--->" + Build.SUPPORTED_64_BIT_ABIS);
        LogUtils.e(TAG, "Build.TYPE--->" + Build.TYPE);
        LogUtils.e(TAG, "Build.TAGS--->" + Build.TAGS);
        LogUtils.e(TAG, "Build.FINGERPRINT--->" + Build.FINGERPRINT);
        LogUtils.e(TAG, "Build.TIME--->" + Build.TIME);
        LogUtils.e(TAG, "Build.USER--->" + Build.USER);
        LogUtils.e(TAG, "Build.HOST--->" + Build.HOST);


//        LogUtils.e(TAG, "StorageManager.ACTION_MANAGE_STORAGE--->" + StorageManager.ACTION_MANAGE_STORAGE);
//        LogUtils.e(TAG, "StorageManager.UUID_DEFAULT--->" + StorageManager.UUID_DEFAULT);
//        LogUtils.e(TAG, "StorageManager.EXTRA_UUID--->" + StorageManager.EXTRA_UUID);
//        LogUtils.e(TAG, "StorageManager.EXTRA_REQUESTED_BYTES--->" + StorageManager.EXTRA_REQUESTED_BYTES);

//        LogUtils.e(TAG, "BatteryManager.ACTION_MANAGE_STORAGE--->" + BatteryManager.ACTION_CHARGING);


    }

}
